﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SAW_Core;
using WLMCoreLibrary;

namespace SAWPOS
{
    public partial class CaptureForm : Form
    {
        List<Producto> listado;
        string[] listaProductos;
        public CaptureForm()
        {
            InitializeComponent();

            PosUpdateService.Producto[] listaServer = new PosUpdateService.POSUpdateService().GetProductList();
            listado = new List<Producto>();
            listaProductos = new string[listaServer.Length];
            int x = 0;
            foreach (PosUpdateService.Producto proser in listaServer)
            {
                Producto prod = new Producto();
                prod.id = proser.id;
                prod.Descripcion = proser.Descripcion;
                prod.Marca = proser.Marca;
                prod.Linea = proser.Linea;
                prod.Clave = proser.Clave;
                prod.Categoria = proser.Categoria;
                prod.Precio1 = proser.Precio1;
                listado.Add(prod);
                listaProductos[x] = proser.Descripcion.ToUpper();
                x++;

            }

        }



        private void UpdateTotals(object sender, EventArgs e)
        {
            UpdateTotals();
        }

        private void txtSubTotal_TextChanged(object sender, EventArgs e)
        {
            UpdateTotals();
        }

        private void txtDescPorc_TextChanged(object sender, EventArgs e)
        {
            UpdateTotals();
        }


        private void gridItems_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
        {
            if (e.Control is DataGridViewTextBoxEditingControl)
            {
                ((TextBox)e.Control).CharacterCasing = CharacterCasing.Upper;
                DataGridViewTextBoxEditingControl te =
                (DataGridViewTextBoxEditingControl)e.Control;
                te.AutoCompleteMode = AutoCompleteMode.Suggest;
                
                te.AutoCompleteSource = AutoCompleteSource.CustomSource;

                te.AutoCompleteCustomSource.AddRange(listaProductos);

            }
        }

        private void gridItems_CellEndEdit(object sender,
    DataGridViewCellEventArgs e)
        {
            DataGridViewCell celda = gridItems[e.ColumnIndex, e.RowIndex];
            try
            {
                //MessageBox.Show(e.ColumnIndex.ToString());
                if (e.ColumnIndex == 4)
                    UpdateTotals();
                else
                {
                    if (celda.Value.ToString() != "")
                    {

                        foreach (Producto it in listado)
                        {
                            if (it.Descripcion.ToUpper() == celda.Value.ToString().ToUpper())
                            {
                                //MessageBox.Show(it.Descripcion+"\n"+it.Categoria);
                                gridItems.Rows[e.RowIndex].Cells["precio"].Value = it.Precio1;
                                gridItems.Rows[e.RowIndex].Cells["importe"].Value = it.Precio1 * 3;
                                gridItems.Rows[e.RowIndex].Cells["cantidad"].Value = 1M;
                                gridItems.Rows[e.RowIndex].Cells["claveItem"].Value = it.Clave.ToUpper();
                                gridItems.Rows[e.RowIndex].Cells["Descripcion"].Value = it.Descripcion;
                                gridItems.Rows[e.RowIndex].Cells["categoria"].Value = it.Categoria.ToUpper();
                                gridItems.Rows[e.RowIndex].Cells["Linea"].Value = it.Linea.ToUpper();
                                gridItems.Rows[e.RowIndex].Cells["marca"].Value = it.Marca.ToUpper();
                                return;
                            }
                        }
                    }
                }
            }
            catch { }
        }

        
        private void UpdateTotals()
        {
            decimal subtotal = 0;
            for (int x = 0; x < gridItems.Rows.Count; x++)
            {

                if (gridItems["precio", x].Value != null)
                {
                    gridItems["importe", x].Value = Convert.ToDecimal(gridItems["cantidad", x].Value) * Convert.ToDecimal(gridItems["precio", x].Value);
                    subtotal += Convert.ToDecimal(gridItems["importe", x].Value.ToString().Replace("$", ""));
                }
            }
            try
            {

                decimal descuentoPorcentaje = Convert.ToDecimal(txtDescPorc.Text);
                decimal subTotal = subtotal - (subtotal * (descuentoPorcentaje / 100));
                decimal iva = subTotal * 0.16M;
                decimal total = subTotal + iva;
                txtSubTotal.Text = subtotal.ToString("C");
                txtDescuento.Text = (subtotal * (descuentoPorcentaje / 100)).ToString("C");
                txtIVA.Text = iva.ToString("C");
                txtTotal.Text = total.ToString("C");

                statusBar.Items[1].Text = total.ToString("C");
            }
            catch { }
        }

        private void cerrarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new ListaVentas().Show();
        }

        private void btnGuardarCompra_Click(object sender, EventArgs e)
        {DataSet ds = new DataSet();
            ds.ReadXml("..\\..\\xmlData\\xmlVentas.xml");
            DataRow data = ds.Tables["Cotizaciones"].NewRow();
            data["id"] = 1;
            data["razonsocial"] = "";
            
            data["subTotal"] = WLMFunctions.MonedaADecimal(txtSubTotal.Text);
            data["Descuento"] = WLMFunctions.MonedaADecimal(txtDescuento.Text);
            data["DescuentoPorc"] = WLMFunctions.MonedaADecimal(txtDescPorc.Text);
            data["total"] = WLMFunctions.MonedaADecimal(txtTotal.Text);

            
            ds.Tables["Cotizaciones"].Rows.Add(data);

            for (int x = 0; x < gridItems.Rows.Count; x++)
            {

                if (gridItems["precio", x].Value != null)
                {
                    DataRow datProd = ds.Tables["CotizacionItems"].NewRow();
                    datProd["idCotizacion"] = data["id"];
                    datProd["Clave"] = gridItems["claveItem", x].Value.ToString();
                    datProd["Descripcion"] = gridItems["Descripcion", x].Value.ToString();
                    datProd["Cantidad"] = gridItems["cantidad", x].Value.ToString();
                    datProd["Precio"] = gridItems["Precio", x].Value.ToString();
                    ds.Tables["CotizacionItems"].Rows.Add(datProd);
                }
            }

            System.IO.StreamWriter sw = new System.IO.StreamWriter("..\\..\\xmlData\\xmlVentas.xml");
            sw.Write(ds.GetXml().ToString()); //place the xml into the text file
sw.Flush();
sw.Close();
        }



    }

   
}
